package com.pengjunlee.hive.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.pengjunlee.hive.bean.LocateInfo;
import com.pengjunlee.hive.bean.VehicleH5Result;
import com.pengjunlee.hive.bean.VehicleResult;
import com.pengjunlee.hive.utils.GCJ02ToWGS84;
import com.pengjunlee.hive.utils.JedisUtils;

import redis.clients.jedis.Jedis;


@Service
public class JedisService implements EnvironmentAware{
	
	private Jedis jedis;
	
	@SuppressWarnings("unused")
	@Autowired
	private Environment env;
	
	@Override
	public void setEnvironment(Environment env) {
		this.env = env;
	}
	
	/**
	 * 根据终端号码获取车辆实时信息
	 * @param terminalCodes
	 * @return
	 * @throws Exception 
	 * @throws InterruptedException
	 */
	public List<VehicleH5Result> queryVehicleInfoByTerminalCode(String terminalCodes) throws Exception {
		List<VehicleH5Result> list = new ArrayList<VehicleH5Result>();
		//判断设备号是否为空
		if(StringUtils.isBlank(terminalCodes)){
			throw new Exception("请输入设备号!");
		}
		
		try{
			//解析设备号字符串
			String[] terminalCodeArray = terminalCodes.split(",");
			if(terminalCodeArray != null && terminalCodeArray.length > 0){
				
				//redis初始化
				jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
						Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
				jedis.select(2);
				//获取所有的vin
				Set<String> sets = jedis.keys("*");
				for(int index=0; index<terminalCodeArray.length; index++){
					String terminalCode = StringUtils.EMPTY;
					if(sets != null && sets.size() > 0){
						for(String vin : sets){
							if(terminalCodeArray[index].equals(vin)){
								terminalCode = vin;
							}
						}
					}
					
					Set<String> keySet = jedis.hkeys(terminalCode);
					
					//车辆时间戳列表
					List<String> lastRefreshTimeList = new ArrayList<String>();
					
					if(keySet != null && keySet.size() > 0){
						for(String lastRefreshTime : keySet){
							lastRefreshTimeList.add(lastRefreshTime);
						}
						if(lastRefreshTimeList != null && lastRefreshTimeList.size() > 0){
							Collections.sort(lastRefreshTimeList);
						}
					}
					
					if(lastRefreshTimeList != null && lastRefreshTimeList.size() > 0){
						for(int i=lastRefreshTimeList.size()-1; i>=0; i--){
							String finalTime = lastRefreshTimeList.get(i);
							String value = jedis.hget(terminalCodeArray[index], finalTime);
							if(StringUtils.isBlank(value) || value.equals("{}")){
								continue;
							}else{
								VehicleH5Result vehicle = this.parseEHS3RealJson2Object(terminalCodeArray[index], finalTime.toString(), value);
								if(vehicle == null){
									continue;
								}else{
									list.add(vehicle);
									//向缓存表中增加数据
									//this.updateVinCache(terminalCodeArray[index], finalTime.toString(), value);
									break;
								}
							}
						}
					}
				}
			}
		
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			jedis.close();
		}
		return list;
		
		
		
	}
	
	/**
	 * 根据终端号码获取车辆实时信息
	 * @param terminalCodes
	 * @return
	 * @throws Exception 
	 * @throws InterruptedException
	 */
	public List<VehicleH5Result> queryEHS3VehicleInfoByTerminalCode(String terminalCodes) throws Exception {
		List<VehicleH5Result> list = new ArrayList<VehicleH5Result>();
		//判断设备号是否为空
		if(StringUtils.isBlank(terminalCodes)){
			throw new Exception("请输入设备号!");
		}
		
		//解析设备号字符串
		String[] terminalCodeArray = terminalCodes.split(",");
		if(terminalCodeArray != null && terminalCodeArray.length > 0){
			//redis初始化
			jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
					Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
			jedis.select(1);
			
			for(int index=0; index<terminalCodeArray.length; index++){
				//获取redis中所有的所有的key值
				String value = jedis.hget("tm30_status_ehs3_VIN_pcol_realtime", terminalCodeArray[index]);
				VehicleH5Result vehicle = this.parseEHS3RealJson2Object(terminalCodeArray[index], value);
				if(vehicle != null){
					list.add(vehicle);
				}
			}
		}
			
		return list;
	}
	
	/**
	 * 根据设备号获取设备信息从缓存表
	 * @param terminalCode
	 * @return
	 * @throws Exception 
	 */
	public VehicleH5Result queryVehicleInfoByTerminalCodeFromCache(String terminalCode) throws Exception{
		if(StringUtils.isBlank(terminalCode)){
			return null;
		}
		
		//redis初始化
		jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
				Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
		jedis.select(0);
		
		//根据设备号从缓存表中获取设备的实时信息
		String value = jedis.hget("tm30_status_ehs3_vin_value_cache", terminalCode);
		//根据设备号从缓存表中获取设备的实时时间
		String finalTime = jedis.hget("tm30_status_ehs3_vin_timestamp_cache", terminalCode);
		//获取ehs3当前设备的实时信息
		VehicleH5Result vehicle = this.parseEHS3RealJson2Object(terminalCode, finalTime, value);
		//释放资源
		JedisUtils.returnResource(jedis);
		return vehicle;
	}
	
	/**
	 * 根据设备号更新缓存表
	 * @param terminalCode
	 * @return
	 * @throws Exception
	 */
	public void updateVinCache(String terminalCode, String finalTime, String value) throws Exception{
		if(StringUtils.isBlank(terminalCode)){
			return;
		}
		
		//去掉多余的u
		if(StringUtils.isNotBlank(value)){
			value = value.replace("u", StringUtils.EMPTY);
		}
		
		//redis初始化
		jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
				Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
		jedis.select(0);
		
		//首先判断设备号在缓存中是否存在
		boolean valueIsExist = jedis.hexists("tm30_status_ehs3_vin_value_cache", terminalCode);
		boolean stampIsExist = jedis.hexists("tm30_status_ehs3_vin_value_timestamp_cache", finalTime);
		if(valueIsExist){
			//删除实时信息缓存
			jedis.hdel("tm30_status_ehs3_vin_value_cache", terminalCode);
			//删除实时时间戳缓存
			jedis.hdel("tm30_status_ehs3_vin_timestamp_cache", terminalCode);
			//新增实时信息
			jedis.hset("tm30_status_ehs3_vin_value_cache", terminalCode, value);
	 		//新增时间戳缓存
			jedis.hset("tm30_status_ehs3_vin_timestamp_cache", terminalCode, finalTime);
		}else{
			//新增实时信息
			jedis.hset("tm30_status_ehs3_vin_value_cache", terminalCode, value);
	 		//新增时间戳缓存
			jedis.hset("tm30_status_ehs3_vin_timestamp_cache", terminalCode, finalTime);
		}
		//释放资源
		JedisUtils.returnResource(jedis);
	}
	
	/**
	 * 缓存表中新增缓存数据
	 * @param terminalCode
	 * @param value
	 * @throws Exception
	 */
	public void insertVinCache(String terminalCode, String finalTime, String value) throws Exception{
		if(StringUtils.isBlank(terminalCode)){
			return;
		}
		//redis初始化
		jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
				Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
		jedis.select(0);
		jedis.hdel(terminalCode, terminalCode);
		//新增实时信息
		jedis.hset("tm30_status_ehs3_vin_value_cache", terminalCode, value);
 		//新增时间戳缓存
		jedis.hset("tm30_status_ehs3_vin_timestamp_cache", terminalCode, finalTime);
		//释放资源
		JedisUtils.returnResource(jedis);
	}
	
	/**
	 * 根据终端号码获取H5车辆实时信息
	 * @param terminalCodes
	 * @return
	 * @throws Exception 
	 * @throws InterruptedException
	 */
	public List<VehicleH5Result> queryH5VehicleInfoByTerminalCode(String terminalCodes) throws Exception  {
		List<VehicleH5Result> list = new ArrayList<VehicleH5Result>();
		//判断设备号是否为空
		if(StringUtils.isBlank(terminalCodes)){
			throw new Exception("请输入设备号!");
		}
		
		try{
			//解析设备号字符串
			String[] terminalCodeArray = terminalCodes.split(",");
			if(terminalCodeArray != null && terminalCodeArray.length > 0){
				Set<String> sets = null;
				//redis初始化
				for(int index=0; index<terminalCodeArray.length; index++){
					Jedis jedis1 = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
							Integer.parseInt(env.getProperty("spring.redis.port")));
					
					
					
					
					
					//通过设备终端号从缓存表中获取设备的最新状态的json
					jedis1.select(0);
					String realValue = jedis1.hget("tm30_status_ehs3_vin_value_cache", terminalCodeArray[index]);
					String realTime = jedis1.hget("tm30_status_ehs3_vin_timestamp_cache", terminalCodeArray[index]);
					if(jedis1.isConnected()){
						//释放资源
						JedisUtils.destroy();
					}
					//释放资源
					//JedisUtils.returnResource(jedis);
					
					//如果缓存表中有这个vin的状态数据
					if(StringUtils.isNotBlank(realValue)){
						VehicleH5Result vr = this.parseEHS3RealJson2Object(terminalCodeArray[index], realTime, realValue);
						if(vr != null){
							list.add(vr);
							continue;
						}
					}
					
					//获取redis中所有的所有的key值
					if(sets == null){
						//if(!jedis1.isConnected()){
							//redis初始化
							Jedis jedis11 = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
									Integer.parseInt(env.getProperty("spring.redis.port")));
						//}
						jedis11.select(1);
						sets = jedis11.hkeys("YDCX_TM30_EHS3_STATUS_OFFSET");
//						if(jedis11.isConnected()){
							jedis11.disconnect();
							//jedis1.close();
							JedisUtils.destroy();
//						}
						
						//释放资源
						//JedisUtils.returnResource(jedis);
					}
					
					//车辆时间戳列表
					List<Long> lastRefreshTimeList = new ArrayList<Long>();
					
					for(String str : sets){
						String tc = str.split("-")[0];
						String tcTime = str.split("-")[1];
						if(tc.equals(terminalCodeArray[index])){
							lastRefreshTimeList.add(Long.parseLong(tcTime));
						}
					}
					//时间戳列表排序
					if(lastRefreshTimeList != null && lastRefreshTimeList.size() > 0){
						Collections.sort(lastRefreshTimeList);
					}
					
					if(lastRefreshTimeList != null && lastRefreshTimeList.size() > 0){
						for(int i=lastRefreshTimeList.size()-1; i>=0; i--){
							Long finalTime = lastRefreshTimeList.get(i);
							String hkey = terminalCodeArray[index] + "-" + finalTime.toString();
							
							//redis初始化
							Jedis jedis3 = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
									Integer.parseInt(env.getProperty("spring.redis.port")));
							jedis3.select(1);
							String value = jedis3.hget("YDCX_TM30_EHS3_STATUS_OFFSET", hkey);
//							if(jedis3.isConnected()){
								jedis3.disconnect();
								JedisUtils.destroy();
//							}
							
							//释放资源
							//JedisUtils.returnResource(jedis);
							
							if(StringUtils.isBlank(value) || value.equals("{}")){
								continue;
							}else{
								VehicleH5Result vehicle = this.parseEHS3RealJson2Object(terminalCodeArray[index], finalTime.toString(), value);
								if(vehicle == null){
									continue;
								}else{
									list.add(vehicle);
									//向缓存表中增加数据
									this.updateVinCache(terminalCodeArray[index], finalTime.toString(), value);
									break;
								}
							}
						}
					}
				}
			}
		
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			
		}
		//释放资源
		//JedisUtils.returnResource(jedis);
		return list;
	}
	
	
	/**
	 * 将redis中的value解析为vehicle对象
	 * @param terminalCode
	 * @param lastRefreshTime
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	public VehicleResult parseJson2Object(String terminalCode, String lastRefreshTime, String jsonStr) throws Exception{
		VehicleResult vehicle = new VehicleResult();
		//判断value值是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("没有终端号!");
		}
		
		if(StringUtils.isBlank(jsonStr) || jsonStr.equals("{}")){
			return null;
		}
		
		//去掉多余的u
		String jsonResult = jsonStr.replace("u", StringUtils.EMPTY);
		//拼装数据对象
		if(StringUtils.isNotBlank(jsonStr)){
			//将字符串转换为json对象
			JSONObject jsonVehicle = JSONObject.parseObject(jsonResult);
			vehicle.setTerminalCode(terminalCode);
			
			try{
				
				if(StringUtils.isNotBlank(jsonVehicle.getString("GPS002"))){
					String lgt = JSONObject.parseObject(jsonVehicle.getString("GPS002")).getString("val");
					vehicle.setLgt(Double.parseDouble(lgt));
				}else{
					return null;
					//vehicle.setLgt(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicle.getString("GPS003"))){
					String lat = JSONObject.parseObject(jsonVehicle.getString("GPS003")).getString("val");
					vehicle.setLat(Double.parseDouble(lat));
				}else{
					return null;
					//vehicle.setLat(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicle.getString("GPS006"))){
					String angle = JSONObject.parseObject(jsonVehicle.getString("GPS006")).getString("val");
					vehicle.setAngle(Double.parseDouble(angle));
				}else{
					vehicle.setAngle(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicle.getString("V019"))){
					String speed = JSONObject.parseObject(jsonVehicle.getString("V019")).getString("val");
					vehicle.setSpeed(Double.parseDouble(speed));
				}else{
					vehicle.setSpeed(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicle.getString("V015"))){
					String totalKm = JSONObject.parseObject(jsonVehicle.getString("V015")).getString("val");
					vehicle.setTotalKm(Double.parseDouble(totalKm));
				}else{
					vehicle.setTotalKm(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicle.getString("V133"))){
					String enableKm = JSONObject.parseObject(jsonVehicle.getString("V133")).getString("val");
					vehicle.setEnableKm(enableKm);
				}else{
				}
				if(StringUtils.isNotBlank(jsonVehicle.getString("E010"))){
					String powerPer = JSONObject.parseObject(jsonVehicle.getString("E010")).getString("val");
					vehicle.setSoc(Integer.parseInt(powerPer));
				}else{
					vehicle.setSoc(0);
				}
				
				vehicle.setLastRefreshTime(lastRefreshTime);
			}catch(Exception ex){
				throw new Exception(ex.getMessage());
			}
		}
		return vehicle;
	}
	
	/**
	 * 将redis中的value解析为H5vehicle对象
	 * @param terminalCode
	 * @param lastRefreshTime
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	public VehicleH5Result parseH5Json2Object(String terminalCode, String lastRefreshTime, String jsonStr) throws Exception{
		VehicleH5Result vehicle = new VehicleH5Result();
		//判断value值是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("没有终端号!");
		}
		
		if(StringUtils.isBlank(jsonStr) || jsonStr.equals("{}")){
			return null;
		}
		
		//去掉多余的u
		String jsonResult = jsonStr.replace("u", StringUtils.EMPTY);
		//拼装数据对象
		if(StringUtils.isNotBlank(jsonStr)){
			//将字符串转换为json对象
			JSONObject jsonVehicle = JSONObject.parseObject(jsonResult);
			vehicle.setTerminalCode(terminalCode);
			
			try{
				//获取eventCode
				String eventCode = jsonVehicle.getString("event_code");
				if(StringUtils.isNotBlank(eventCode)){
					if(eventCode.equals("DRIVETIMER")){
						vehicle.setEventCode(eventCode);
						JSONObject jsonVehicleData = (JSONObject)jsonVehicle.get("data");
						
						if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS002"))){
							String lgt = JSONObject.parseObject(jsonVehicleData.getString("GPS002")).getString("val");
							vehicle.setLgt(Double.parseDouble(lgt));
						}else{
							return null;
							//vehicle.setLgt(Double.parseDouble("0"));
						}
						if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS003"))){
							String lat = JSONObject.parseObject(jsonVehicleData.getString("GPS003")).getString("val");
							vehicle.setLat(Double.parseDouble(lat));
						}else{
							return null;
							//vehicle.setLat(Double.parseDouble("0"));
						}
						if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS006"))){
							String angle = JSONObject.parseObject(jsonVehicleData.getString("GPS006")).getString("val");
							vehicle.setAngle(Double.parseDouble(angle));
						}else{
							vehicle.setAngle(Double.parseDouble("0"));
						}
						if(StringUtils.isNotBlank(jsonVehicleData.getString("V019"))){
							String speed = JSONObject.parseObject(jsonVehicleData.getString("V019")).getString("val");
							vehicle.setSpeed(Double.parseDouble(speed));
						}else{
							vehicle.setSpeed(Double.parseDouble("0"));
						}
						if(StringUtils.isNotBlank(jsonVehicleData.getString("V015"))){
							String totalKm = JSONObject.parseObject(jsonVehicleData.getString("V015")).getString("val");
							vehicle.setTotalKm(Double.parseDouble(totalKm));
						}else{
							vehicle.setTotalKm(Double.parseDouble("0"));
						}
						if(StringUtils.isNotBlank(jsonVehicleData.getString("V133"))){
							String enableKm = JSONObject.parseObject(jsonVehicleData.getString("V133")).getString("val");
							vehicle.setEnableKm(enableKm);
						}else{
							//vehicle.setEnableKm("-");
						}
						if(StringUtils.isNotBlank(jsonVehicleData.getString("E010"))){
							String powerPer = JSONObject.parseObject(jsonVehicleData.getString("E010")).getString("val");
							vehicle.setSoc(Integer.parseInt(powerPer));
						}else{
							vehicle.setSoc(0);
						}
						
						vehicle.setLastRefreshTime(lastRefreshTime);
					}else{
						return null;
					}
				}else{
					return null;
				}
			}catch(Exception ex){
				throw new Exception(ex.getMessage());
			}
		}
		return vehicle;
	}
	
	/**
	 * 获取ehs3当前设备的实时信息(新)
	 * @param terminalCode
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	public VehicleH5Result parseEHS3RealJson2Object(String terminalCode, String jsonStr) throws Exception{
		VehicleH5Result vehicle = new VehicleH5Result();
		//判断value值是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("没有终端号!");
		}
		
		if(StringUtils.isBlank(jsonStr) || jsonStr.equals("{}")){
			return null;
		}
		
		//去掉多余的u
		String jsonResult = jsonStr.replace("u", StringUtils.EMPTY);
		
		if(StringUtils.isNotBlank(jsonStr)){
			//将字符串转换为json对象
			JSONObject jsonVehicle = JSONObject.parseObject(jsonResult);
			vehicle.setTerminalCode(terminalCode);
			
			try{
				//获取eventCode
				String eventCode = jsonVehicle.getString("event_code");
				
				if(eventCode.equals("DRIVETIMER") || eventCode.equals("TURNSTART") || eventCode.equals("TURNEND") || eventCode.equals("POSITIONANDSPEED")){
					vehicle.setEventCode(eventCode);
					
					//设备上传时间
					String lastRefreshTime = jsonVehicle.getString("pkg_ts");
					vehicle.setLastRefreshTime(lastRefreshTime);
					
					String lgt = StringUtils.EMPTY;
					String lat = StringUtils.EMPTY;
					
					JSONObject jsonVehicleData = (JSONObject)jsonVehicle.get("data");
					if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS002"))){
						lgt = JSONObject.parseObject(jsonVehicleData.getString("GPS002")).getString("val");
						vehicle.setLgt(Double.parseDouble(lgt));
					}else{
						return null;
					}
					if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS003"))){
						lat = JSONObject.parseObject(jsonVehicleData.getString("GPS003")).getString("val");
						vehicle.setLat(Double.parseDouble(lat));
					}else{
						return null;
					}
	
					//经纬度由GCLJ02转换为WGS84体系
					LocateInfo locateInfo = GCJ02ToWGS84.gcj02ToWgs84(Double.parseDouble(lat), Double.parseDouble(lgt));
					vehicle.setLat(locateInfo.getLatitude());
					vehicle.setLgt(locateInfo.getLongitude());
					
					if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS006"))){
						String angle = JSONObject.parseObject(jsonVehicleData.getString("GPS006")).getString("val");
						vehicle.setAngle(Double.parseDouble(angle));
					}else{
						vehicle.setAngle(Double.parseDouble("0"));
					}
					if(StringUtils.isNotBlank(jsonVehicleData.getString("V019"))){
						String speed = JSONObject.parseObject(jsonVehicleData.getString("V019")).getString("val");
						vehicle.setSpeed(Double.parseDouble(speed));
					}else{
						vehicle.setSpeed(Double.parseDouble("0"));
					}
					if(StringUtils.isNotBlank(jsonVehicleData.getString("V015"))){
						String totalKm = JSONObject.parseObject(jsonVehicleData.getString("V015")).getString("val");
						vehicle.setTotalKm(Double.parseDouble(totalKm));
					}else{
						vehicle.setTotalKm(Double.parseDouble("0"));
					}
					if(StringUtils.isNotBlank(jsonVehicleData.getString("V133"))){
						String enableKm = JSONObject.parseObject(jsonVehicleData.getString("V133")).getString("val");
						vehicle.setEnableKm(enableKm);
					}else{
						return null;
					}
					if(StringUtils.isNotBlank(jsonVehicleData.getString("E010"))){
						String powerPer = JSONObject.parseObject(jsonVehicleData.getString("E010")).getString("val");
						vehicle.setSoc(Integer.parseInt(powerPer));
					}else{
						// vehicle.setSoc(0);
						return null;
					}
			}else{
				return null;
			}
				
			}catch(Exception ex){
				throw new Exception(ex.getMessage());
			}
		}
		return vehicle;
	}
	
	/**
	 * 获取ehs3当前设备的实时信息
	 * @param terminalCode
	 * @param lastRefreshTime
	 * @param jsonStr
	 * @return
	 * @throws Exception
	 */
	public VehicleH5Result parseEHS3RealJson2Object(String terminalCode, String lastRefreshTime, String jsonStr) throws Exception{
		VehicleH5Result vehicle = new VehicleH5Result();
		//判断value值是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("没有终端号!");
		}
		
		if(StringUtils.isBlank(jsonStr) || jsonStr.equals("{}")){
			return null;
		}
		
		//去掉多余的u
		String jsonResult = jsonStr.replace("u", StringUtils.EMPTY);
		//拼装数据对象
		if(StringUtils.isNotBlank(jsonStr)){
			//将字符串转换为json对象
			JSONObject jsonVehicle = JSONObject.parseObject(jsonResult);
			vehicle.setTerminalCode(terminalCode);
			
			try{
				//获取eventCode
				String eventCode = jsonVehicle.getString("event_code");
				vehicle.setEventCode(eventCode);
				
				String lgt = StringUtils.EMPTY;
				String lat = StringUtils.EMPTY;
				
				JSONObject jsonVehicleData = (JSONObject)jsonVehicle.get("data");
				if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS002"))){
					lgt = JSONObject.parseObject(jsonVehicleData.getString("GPS002")).getString("val");
					vehicle.setLgt(Double.parseDouble(lgt));
				}else{
					return null;
				}
				if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS003"))){
					lat = JSONObject.parseObject(jsonVehicleData.getString("GPS003")).getString("val");
					vehicle.setLat(Double.parseDouble(lat));
				}else{
					return null;
				}

				//经纬度由GCLJ02转换为WGS84体系
				LocateInfo locateInfo = GCJ02ToWGS84.gcj02ToWgs84(Double.parseDouble(lat), Double.parseDouble(lgt));
				vehicle.setLat(locateInfo.getLatitude());
				vehicle.setLgt(locateInfo.getLongitude());
				
				if(StringUtils.isNotBlank(jsonVehicleData.getString("GPS006"))){
					String angle = JSONObject.parseObject(jsonVehicleData.getString("GPS006")).getString("val");
					vehicle.setAngle(Double.parseDouble(angle));
				}else{
					vehicle.setAngle(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicleData.getString("V019"))){
					String speed = JSONObject.parseObject(jsonVehicleData.getString("V019")).getString("val");
					vehicle.setSpeed(Double.parseDouble(speed));
				}else{
					vehicle.setSpeed(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicleData.getString("V015"))){
					String totalKm = JSONObject.parseObject(jsonVehicleData.getString("V015")).getString("val");
					vehicle.setTotalKm(Double.parseDouble(totalKm));
				}else{
					vehicle.setTotalKm(Double.parseDouble("0"));
				}
				if(StringUtils.isNotBlank(jsonVehicleData.getString("V133"))){
					String enableKm = JSONObject.parseObject(jsonVehicleData.getString("V133")).getString("val");
					vehicle.setEnableKm(enableKm);
				}else{
					return null;
				}
				if(StringUtils.isNotBlank(jsonVehicleData.getString("E010"))){
					String powerPer = JSONObject.parseObject(jsonVehicleData.getString("E010")).getString("val");
					vehicle.setSoc(Integer.parseInt(powerPer));
				}else{
					return null;
				}
				
				vehicle.setLastRefreshTime(lastRefreshTime.substring(0, 18));
				
			}catch(Exception ex){
				throw new Exception(ex.getMessage());
			}
		}
		return vehicle;
	}
	
	/**
	 * 根据终端号获取车辆轨迹信息
	 * @param terminalCode
	 * @return
	 * @throws Exception 
	 */
	public List<VehicleResult> queryVehicleTrackInfoByTerminalCode(String terminalCode, String startTime, String endTime) throws Exception{
		List<VehicleResult> list = new ArrayList<VehicleResult>();
		//判断设备号是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("请输入设备号!");
		}
		//判断开始时间是否为空
		if(StringUtils.isBlank(startTime)){
			throw new Exception("请输入开始时间!");
		}
		//判断结束时间是否为空
		if(StringUtils.isBlank(endTime)){
			throw new Exception("请输入结束时间!");
		}
		
		SimpleDateFormat sdfStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long startTimeLong = sdfStart.parse(startTime).getTime();
		
		SimpleDateFormat sdfEnd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long endTimeLong = sdfEnd.parse(endTime).getTime();
		
		//redis初始化
		//jedis = JedisUtils.getResource();
		jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
				Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
		//获取redis中所有的所有的key值
		Set<String> sets = jedis.hkeys("tm30_status_ehs3");
		
		//车辆时间戳列表
		List<Long> lastRefreshTimeList = new ArrayList<Long>();
		
		for(String str : sets){
			String tc = str.split("-")[0];
			String tcTime = str.split("-")[1];
			if(tc.equals(terminalCode)){
				lastRefreshTimeList.add(Long.parseLong(tcTime));
			}
		}
		//根据时间过滤后的时间戳列表
		List<Long> filterTimeList = new ArrayList<Long>();
		
		if(lastRefreshTimeList != null && lastRefreshTimeList.size() >0){
			for(Long ts : lastRefreshTimeList){
				if(ts > startTimeLong && ts < endTimeLong){
					filterTimeList.add(ts);
				}
			}
		}
		
		//时间戳列表排序
		if(filterTimeList != null && filterTimeList.size() > 0){
			Collections.sort(filterTimeList);
		}
		
		if(filterTimeList != null && filterTimeList.size() > 0){
			for(Long ts : filterTimeList){
				String hkey = terminalCode + "-" + ts.toString();
				String value = jedis.hget("tm30_status_ehs3", hkey);
				VehicleResult vehicle = this.parseJson2Object(terminalCode, ts.toString(), value);
				if(vehicle != null){
					list.add(vehicle);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * 根据终端号获取车辆轨迹信息
	 * @param terminalCode
	 * @return
	 * @throws Exception 
	 */
	public List<VehicleH5Result> queryH5VehicleTrackInfoByTerminalCode(String terminalCode, String startTime, String endTime) throws Exception{
		List<VehicleH5Result> list = new ArrayList<VehicleH5Result>();
		//判断设备号是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("请输入设备号!");
		}
		//判断开始时间是否为空
		if(StringUtils.isBlank(startTime)){
			throw new Exception("请输入开始时间!");
		}
		//判断结束时间是否为空
		if(StringUtils.isBlank(endTime)){
			throw new Exception("请输入结束时间!");
		}
		
		SimpleDateFormat sdfStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long startTimeLong = sdfStart.parse(startTime).getTime();
		
		SimpleDateFormat sdfEnd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long endTimeLong = sdfEnd.parse(endTime).getTime();
		
		//redis初始化
		jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
				Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));
		//获取redis中所有的所有的key值
		jedis.select(1);
		Set<String> sets = jedis.hkeys("YDCX_TM30_EHS3_STATUS_OFFSET");
		
		//车辆时间戳列表
		List<Long> lastRefreshTimeList = new ArrayList<Long>();
		
		for(String str : sets){
			String tc = str.split("-")[0];
			String tcTime = str.split("-")[1];
			if(tc.equals(terminalCode)){
				lastRefreshTimeList.add(Long.parseLong(tcTime));
			}
		}
		//根据时间过滤后的时间戳列表
		List<Long> filterTimeList = new ArrayList<Long>();
		
		if(lastRefreshTimeList != null && lastRefreshTimeList.size() >0){
			for(Long ts : lastRefreshTimeList){
				if(ts > startTimeLong && ts < endTimeLong){
					filterTimeList.add(ts);
				}
			}
		}
		
		//时间戳列表排序
		if(filterTimeList != null && filterTimeList.size() > 0){
			Collections.sort(filterTimeList);
		}
		
		if(filterTimeList != null && filterTimeList.size() > 0){
			for(Long ts : filterTimeList){
				String hkey = terminalCode + "-" + ts.toString();
				String value = jedis.hget("YDCX_TM30_EHS3_STATUS_OFFSET", hkey);
				VehicleH5Result vehicle = this.parseH5Json2Object(terminalCode, ts.toString(), value);
				if(vehicle != null){
					list.add(vehicle);
				}
			}
		}
		
		return list;
	}
	
	/**
	 * 根据终端号和开始结束时间获取EHS3设备的轨迹信息
	 * @param terminalCode
	 * @param startTime
	 * @param endTime
	 * @return
	 * @throws Exception
	 */
	public List<VehicleH5Result> queryEHS3VehicleTrackInfoByTerminalCode(String terminalCode, String startTime, String endTime) throws Exception{
		List<VehicleH5Result> list = new ArrayList<VehicleH5Result>();
		//判断设备号是否为空
		if(StringUtils.isBlank(terminalCode)){
			throw new Exception("请输入设备号!");
		}
		//判断开始时间是否为空
		if(StringUtils.isBlank(startTime)){
			throw new Exception("请输入开始时间!");
		}
		//判断结束时间是否为空
		if(StringUtils.isBlank(endTime)){
			throw new Exception("请输入结束时间!");
		}
		
		SimpleDateFormat sdfStart = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long startTimeLong = sdfStart.parse(startTime).getTime();
		String startTimeLongStr = startTimeLong.toString() + "000000";
		Long startTimeLongNew = Long.parseLong(startTimeLongStr);
		
		SimpleDateFormat sdfEnd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Long endTimeLong = sdfEnd.parse(endTime).getTime();
		String endTimeLongStr = endTimeLong.toString() + "000000";
		Long endTimeLongNew = Long.parseLong(endTimeLongStr);
		
		try{
			//redis初始化
			jedis = JedisUtils.getResource(env.getProperty("spring.redis.host"), 
			Integer.parseInt(env.getProperty("spring.redis.port")), env.getProperty("spring.redis.password"));

			//获取redis中所有的所有的key值
			jedis.select(2);
			Set<String> sets = jedis.keys("*");
			if(sets.size() > 0){
				for(String str : sets){
					if(terminalCode.equals(str)){
						Set<String> vehicleSets = jedis.hkeys(terminalCode);
						if(vehicleSets.size() > 0){
							for(String vts : vehicleSets){
								Long time1 = Long.parseLong(vts.substring(0,19));
								Long time2 = Long.parseLong(vts.substring(19,38));
								String value = jedis.hget(terminalCode, vts);
								if(time1 > startTimeLongNew && time1 < endTimeLongNew){
									VehicleH5Result vehicle = this.parseEHS3RealJson2Object(terminalCode, value);
									if(vehicle != null){
										list.add(vehicle);
									}
								}else if(time2 > startTimeLongNew && time2 < endTimeLongNew){
									VehicleH5Result vehicle = this.parseEHS3RealJson2Object(terminalCode, value);
									if(vehicle != null){
										list.add(vehicle);
									}
								}
							}
						}
					}
				}
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}finally{
			//释放资源
			jedis.close();
		}		
		return list;
	}
}